package com.uzai.console.service.wechatwork.impl;

import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.dto.wechatwork.wxq.wxgroup.*;
import com.uzai.console.service.wechatwork.WxWorkGroupService;
import com.uzai.console.service.wechatwork.WxWorkGroupSyncService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 企业微信-微信群批量操作
 * @author liuqi
 * @date 2021年08月13日 11:34
 */
@Service
public class WxWorkGroupServiceSyncImpl implements WxWorkGroupSyncService {

    Logger logger = LoggerFactory.getLogger(WxWorkGroupServiceSyncImpl.class);

    @Autowired
    private WxWorkGroupService wxWorkGroupService;

    /**
     * 批量退群
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param wxWorkGroupIdListDto
     * @return null
     */
    @Async("taskExecutor")
    public void quitWxWorkGroupBatch(WxWorkGroupIdListDto wxWorkGroupIdListDto){
        List<String> idList = wxWorkGroupIdListDto.getIdList();
        if(idList == null || idList.size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择群");
        }
        //发送指令延迟时间
        int delay = 0;
        for (String id: idList){
            try {
                WxWorkGroupIdDto wxWorkGroupIdDto = new WxWorkGroupIdDto();
                wxWorkGroupIdDto.setMerId(wxWorkGroupIdListDto.getMerId());
                wxWorkGroupIdDto.setId(id);
                wxWorkGroupIdDto.setDelay(delay);
                //每条指令延迟3秒钟
                delay = delay + 3;
                wxWorkGroupService.quitWxWorkGroup(wxWorkGroupIdDto);
            }catch (Exception e){

            }
        }
    }

    /**
     * 批量发送消息
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param wxWorkGroupSendMessageBatchDto
     * @return null
     */
    @Async("taskExecutor")
    public void sendMessageBatch(WxWorkGroupSendMessageBatchDto wxWorkGroupSendMessageBatchDto){
        List<String> idList = wxWorkGroupSendMessageBatchDto.getIdList();
        if(idList == null || idList.size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择群");
        }

        //异步操作
        for (String id: idList){
            try {
                WxWorkGroupSendMessageDto wxWorkGroupSendMessageDto = new WxWorkGroupSendMessageDto();
                wxWorkGroupSendMessageDto.setMerId(wxWorkGroupSendMessageBatchDto.getMerId());
                wxWorkGroupSendMessageDto.setId(id);
                wxWorkGroupSendMessageDto.setMessage(wxWorkGroupSendMessageBatchDto.getMessage());
                wxWorkGroupService.sendMessage(wxWorkGroupSendMessageDto);
            }catch (Exception e){

            }
        }
    }

    /**
     * 批量刷新群成员列表
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param wxWorkGroupIdListDto
     * @return null
     */
    @Async("taskExecutor")
    public void refreshGroupMemberBatch(WxWorkGroupIdListDto wxWorkGroupIdListDto){
        List<String> idList = wxWorkGroupIdListDto.getIdList();
        if(idList == null || idList.size() == 0){
            throw  new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "请选择群");
        }
        //异步操作
        for (String id: idList){
            try {
                WxWorkGroupIdDto wxWorkGroupIdDto = new WxWorkGroupIdDto();
                wxWorkGroupIdDto.setMerId(wxWorkGroupIdListDto.getMerId());
                wxWorkGroupIdDto.setId(id);
                wxWorkGroupService.refreshGroupMember(wxWorkGroupIdDto);
            }catch (Exception e){

            }
        }
    }

}
